<?php 
class MY_Model extends CI_Model{
    
    protected $_table_name=array();
    protected $_primary_key='id';
    protected $_filter='intval';
    protected $_order_by='';
    
    function __construct(){
        parent::__construct();
        $this->load->database();  
    }
    /**
     * ma hoa chuoi dc chuyen vam
     * @var $string
     */
    public function cry($string)
    {
       $string =  crypt($string,'$2a$09$f.ckH2shS.hNkQ.nYT4i2$');
        return $string; 
    }
    /**
     * Get tat ca gia trị 
     * @var: $id
     * tra ve tat ca gia tri cua bang 
     * tra ve 1 dong neu truyen id
     */ 
    public function get($id = NULL, $single = FALSE,$table_name){
        
        if($id != NULL){
            $filter=$this->_filter;
            $id= $filter($id);
            $this->db->where($this->_primary_key,$id);
            $this->db->limit(1);
            $method='result_array';
            
        }
        elseif($single == TRUE){
            $this->db->limit(1);
            $method='result_array';
        }
        else{
            $method="result_array";
        }
       /* if (!count($this->db->ar_orderby)) {
			$this->db->order_by($this->_order_by);
		}*/
        //$query=$this->db->get($table_name);
        return $this->db->get($table_name)->$method();
    } 
    /**
     * get theo bien dc truyen vao
     * @var: $var = array("field"=>"name of file"), table_name
     */
    public function get_by($var,$table_name){
        $this->db->where($var);
       return $this->get(NULL, true, $table_name);
    }
    public function save($data, $id, $table_name, $prime_key){
        if($id === NULL){
            $this->db->insert($table_name, $data); 
            $id = $this->db->insert_id();
        }
        else{
            $filter=$this->_filter;
            $id=$filter($id);
            $this->db->where($prime_key, $id);
            $this->db->update($table_name, $data); 
        }
        return $id;
    }
    
    public function del($id){
        $filter=$this->_filter;
        $id=$filter($id);
        if(!$id){
            return false;
        }
        $this->db->where($prime_key,$id);
        $this->db->limit(1);
        $this->db->delete($table_name);
    }
}

?>